﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using COMOSOFT.Class;
using DevExpress.XtraGrid.Views.Grid;
using System.IO;

namespace COMOSOFT.WareHouse_Management.Forms
{    
    public partial class FrmImportMA : Form
    {
        ClassShare cls = new ClassShare("FrmImportMA");
        FrmPreviewImportMA frPvMA = null;       
        int MAIID;
        DataTable dtb;
        int BDID, MAID, MADID, MAIDID;
        double ImptQty,UnitPrice;
        List<string> ListItemPv = new List<string>();
        List<object> ListItem = new List<object>();
        string item;
        public FrmImportMA(int _MAIID)
        {
            InitializeComponent();           
            this.MAIID = _MAIID;
            dtb = cls.GetData("sp_WHGetNameWH");
            cls.AutoComplete(txtWareHouse, dtb);
            cls.LoadLayout(gridView_ImportDetails, ClassShare.ftpWareHouse + "/" + ClassShare.UserN + "/" + "LO_MAImportDetails.XML");
        }

        #region[Load Data]
        public void LoadData()
        {
            try
            {
                supplierTableAdapter.Fill(dataSet_WareHouse.Supplier);
                employeeTableAdapter.Fill(dataSet_WareHouse.Employee);
                if (MAIID > 0)
                {
                    groupControl_ImportDetails.Enabled = true;
                    mAImportMasterTableAdapter.Fill(dataSet_WareHouse.MAImportMaster, MAIID);
                    sp_WHImportDetailsTableAdapter.Fill(dataSet_WareHouse.sp_WHImportDetails, MAIID);
                    int SupID = Convert.ToInt32(dataSet_WareHouse.MAImportMaster.Rows[0]["Supplier"]);
                    sp_WHGetBookNoTableAdapter.Fill(this.dataSet_WareHouse.sp_WHGetBookNo, SupID);
                    gridView_ImportDetails.ColumnPanelRowHeight = 20;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void AddNew()
        {
            mAImportMasterBindingSource.AddNew();
            txtCreater.EditValue = Convert.ToInt64(ClassShare.Usid);
            txtIssueDate.EditValue = DateTime.Now.ToShortDateString();
        }
        #endregion

        #region[Update Master]
        public void UpdateData(string txt)
        {
            try
            {
                if (ClassShare.Usid == Convert.ToInt64(txtCreater.EditValue))
                {
                    if (txt == "Update")
                    {
                        mAImportMasterBindingSource.EndEdit();
                        mAImportMasterTableAdapter.Update(dataSet_WareHouse.MAImportMaster);
                        dataSet_WareHouse.MAImportMaster.AcceptChanges();
                        MAIID = Convert.ToInt32(txtMAIID.Text);
                        this.Text = txt_MAINo.Text + "-" + txtSupplier.Text;
                    }
                    else
                    {
                        if (MessageBox.Show("Are you sure delete?", "Comfirm!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {                           
                            cls.UpdateData("sp_WHMAImportMasterDelete",new string[]{"@MAIID"},new object[]{MAIID});                            
                        }
                    }
                    LoadData();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[Close Chose BookNo]
        private void checkedComboBoxBookNo_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            ListItem.Clear();
            if (e.CloseMode.ToString() == "Normal")
            {
                item = null;
                ListItem = checkedComboBoxBookNo.Properties.Items.GetCheckedValues();
                if (ListItem.Count > 0)
                {
                    foreach (object it in ListItem)
                    {
                        item += it.ToString() + ",";
                    }
                    sp_WHImportMATableAdapter.Fill(dataSet_WareHouse.sp_WHImportMA, item);
                    dockPanel_ImportMA.Show();
                }
                else
                    MessageBox.Show("BookNo is empty!");
            }
        }
        #endregion

        #region[Load Booking]
        private void dataNavigator1_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            try
            {
                if (e.Button.Tag.ToString() == "Exit")
                {
                    dockPanel_ImportMA.Close();
                }
                else
                {
                    if (e.Button.Tag.ToString() == "Refresh")
                    {
                        sp_WHImportMATableAdapter.Fill(dataSet_WareHouse.sp_WHImportMA, item);
                    }
                    else
                    {
                        GridView view = gridView_ChooseMA as GridView;
                        int[] ListMA = view.GetSelectedRows();
                        foreach (int i in ListMA)
                        {
                            if (i >= 0)
                            {
                                BDID = Convert.ToInt32(view.GetDataRow(i)["BDID"]);
                                MAID = Convert.ToInt32(view.GetDataRow(i)["MAID"]);
                                MADID = Convert.ToInt32(view.GetDataRow(i)["MADID"]);
                                if (view.GetDataRow(i)["UnitsPrice"].ToString() != "")
                                    UnitPrice = Convert.ToDouble(view.GetDataRow(i)["UnitsPrice"]);
                                if (view.GetDataRow(i)["RemainQty"].ToString() != "")
                                    ImptQty = Convert.ToDouble(view.GetDataRow(i)["RemainQty"]);
                                cls.UpdateData("sp_WHMAImportDetails_Insert", new string[] { "@MAIID", "@BDID", "@MAID", "@MADID", "@UnitPrice", "@ImptQty", "@Remarks", "@IssueDate" }, new object[] { MAIID, BDID, MAID, MADID, UnitPrice, ImptQty, "", DateTime.Now });
                                cls.UpdateData("sp_UpdateStockQuantity", new string[] { "@MADID", "@Qty", "@Flag" }, new object[] { MADID, ImptQty, "Import" });                                
                            }
                        }
                        dockPanel_ImportMA.Close();
                        checkedComboBoxBookNo.Text = null;
                        LoadData();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[Update Details]
        private void gridView_ImportDetails_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            try
            {
                GridView view = sender as GridView;
                if (view.FocusedRowHandle >= 0)
                {
                    MAIDID = Convert.ToInt32(view.GetFocusedDataRow()["MAIDID"]);
                    if (view.GetFocusedDataRow()["ImptQty"].ToString() != "")
                        ImptQty = Convert.ToDouble(view.GetFocusedDataRow()["ImptQty"]);
                    if (view.GetFocusedDataRow()["UnitsPrice"].ToString() != "")
                        UnitPrice = Convert.ToDouble(view.GetFocusedDataRow()["UnitsPrice"]);
                    cls.UpdateData("sp_WHMAImportDetaisl_Update", new string[] { "@MAIDID", "@ImptQty","@UnitPrice" }, new object[] { MAIDID, ImptQty,UnitPrice });
                }
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region [ContextMenu]
        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (gridView_ImportDetails.FocusedRowHandle >= 0)
            {
                if (MessageBox.Show("Are you sure delete?", "Delete Item", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    MAIDID = Convert.ToInt32(gridView_ImportDetails.GetFocusedDataRow()["MAIDID"]);
                    cls.UpdateData("sp_WHMAImportDetals_Delete", new string[] { "@MAIDID" }, new object[] { MAIDID });
                    LoadData();
                }
            }
            else
                MessageBox.Show("No Item to delete!");
        }

        private void saveLayoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (!File.Exists(ClassShare.ftpWareHouse + "/" + ClassShare.UserN))
                    System.IO.Directory.CreateDirectory(ClassShare.ftpWareHouse + "/" + ClassShare.UserN);
                cls.SaveLayout(gridView_ImportDetails, ClassShare.ftpWareHouse + "/" + ClassShare.UserN + "/" + "LO_MAImportDetails.XML");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
       }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void findToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridView_ImportDetails.OptionsFind.AlwaysVisible = true;
        }

        private void notFindToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridView_ImportDetails.OptionsFind.AlwaysVisible = false;
        }
        #endregion

        #region[Preview]
        public void Preview()
        {
            try
            {
                ListItemPv.Clear();
                ListItemPv.Add(txt_MAINo.EditValue.ToString());
                ListItemPv.Add(txtSupplier.EditValue.ToString());
                ListItemPv.Add(txtWareHouse.Text);
                ListItemPv.Add(txtIssueDate.EditValue.ToString());
                ListItemPv.Add(txtCreater.Text.ToString());
                ListItemPv.Add(txtRemarks.EditValue.ToString());
                frPvMA = new FrmPreviewImportMA(gridView_ImportDetails, ListItemPv);
                frPvMA.LoadData();
                frPvMA.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
    }
}
